package org.example.cake.controller;


import afu.org.checkerframework.checker.oigj.qual.O;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.cake.common.Rep;
import org.example.cake.entity.Ordercake;
import org.example.cake.entity.Orderpayinfo;
import org.example.cake.entity.Userdiscount;
import org.example.cake.service.OrdercakeService;
import org.example.cake.service.OrderpayinfoService;
import org.example.cake.service.UserdiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Random;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/orderpayinfo")
public class OrderpayinfoController {
    @Autowired
    OrderpayinfoService orderpayinfoService;
    @Autowired
    UserdiscountService userdiscountService;
    @Autowired
    OrdercakeService ordercakeService;
    /**
     * 添加支付信息和订单信息以及减少优惠券
     * @Date 2025-08-04 谭丽那
     * @param map orderpayinfo 订单信息 （缺少蛋糕id以及订单id） userdiscount 用户连接优惠券
     * @return
     */
    @PostMapping("/savepayinfo")
    public Rep savepayinfo(@RequestBody Map<String,Object> map) {
        System.out.println("map = " + map);
        ObjectMapper mapper = new ObjectMapper();
        Orderpayinfo orderpayinfo = mapper.convertValue(map.get("orderpayinfo"), Orderpayinfo.class);
        Userdiscount userdiscount = mapper.convertValue(map.get("userdiscount"), Userdiscount.class);
        //获取当前时间
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String date = dateTime.format(formatter);
        orderpayinfo.setAdddate(date);
        //添加订单信息
        Integer res = orderpayinfoService.saveOrderpayinfo(orderpayinfo);
        if (res > 0 && userdiscount.getDiscountid() != 0) {
            //修改用户优惠券
            userdiscountService.deldis(userdiscount);
        }
        Random r = new Random();
        int i1 = r.nextInt(999999999);
        //根据时间查询payid
        Integer payId = orderpayinfoService.getorderinfo(date);
        //添加订单数据
        Ordercake ordercake = new Ordercake();
        ordercake.setOrdernum("BK" + i1);
        ordercake.setUserid(userdiscount.getUserid());
        String day = date.substring(0, 11);
        ordercake.setOrdertime(day);
        ordercake.setOrderprice(orderpayinfo.getCountpay());
        ordercake.setOrderstatus(1);
        ordercake.setOrderdiscussif(0);
        ordercake.setPayid(payId);
        ordercake.setOrderecount(orderpayinfo.getStatus());
        Integer i = ordercakeService.addcakeorder(ordercake);
        return new Rep().addData("payId", payId);
    }

    /**
     * 查询订单支付信息
     * @return 订单支付信息
     */
    @GetMapping("/selectOrderpayinfo")
    public Rep selectOrderpayinfo(@RequestParam(required = false)String ordernum,
                                  @RequestParam(required = false)String address,
                                  @RequestParam(required = false)String cakeName,Integer pageNum  , Integer pageSize){
        pageNum  = pageNum  == null ? 1 : pageNum ;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNum , pageSize);
        List<Orderpayinfo> orderpayinfoList = orderpayinfoService.selectOrderpayinfo(ordernum,address,cakeName);
        PageInfo<Orderpayinfo> pageInfo = new PageInfo<>(orderpayinfoList);
        return new Rep().addData("Orderpayinfo",pageInfo);
    }

    /**
     * 删除订单支付信息
     * @return 删除订单支付信息
     */
    @PostMapping("/deleteOrderpayinfo")
    public Rep  deleteOrderpayinfo(Integer payid){
        orderpayinfoService.deleteByPrimaryKey(payid);
        return new Rep().addData("msg","删除订单支付信息成功");
    }

    /**
     * 修改订单备注信息
     * @param
     * @return 操作结果
     */
    @PostMapping("/updateRemark")
    public Rep updateRemark(@RequestParam("payid") Integer payid, @RequestParam("remark") String remark){
        int result = orderpayinfoService.updateRemark(payid,remark);
        if (result > 0) {
            return new Rep().addData("msg", "修改订单备注信息成功");
        } else {
            // 更新失败时返回错误信息
            return new Rep().addData("msg", "修改订单备注信息失败");
        }
    }
}
